package net.pessu.android.shoppoid.wishlist;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBar;
import greendroid.widget.ActionBarItem;
import greendroid.widget.NormalActionBarItem;

import java.util.HashMap;

import net.pessu.android.shoppoid.R;
import net.pessu.android.shoppoid.gui.IhmObjets;
import net.pessu.android.shoppoid.gui.ListeArticles;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class WishList extends GDActivity {

	private static final String ETES_VOUS_SUR_DE_VOULOIR_SUPPRIMER_TOUTE_LA_LISTE_D_ARTICLE = IhmObjets
			.getStringContext(R.string.etesvoussursupprimerliste);
	private static final String NON = IhmObjets.getStringContext(R.string.non);
	private static final String OUI = IhmObjets.getStringContext(R.string.oui);

	private static final String ID2 = "id";

	WishListService service;

	Context context;

	public void onDisplay() {

	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setActionBarContentView(R.layout.wishlist);
		ActionBar bar = getActionBar();
		bar.setTitle(getString(R.string.liste));
		bar.setFont(IhmObjets.tfTitre);
		bar.setFontSize(30);
		addActionBarItem(
				getActionBar().newActionBarItem(NormalActionBarItem.class)
						.setDrawable(R.drawable.gd_action_bar_add)
						.setContentDescription(R.string.add_article),
				R.id.action_bar_add_article);

		context = this;
		service = WishListService.getInstance(this);

		IhmObjets.listeArticles = (ListeArticles) findViewById(R.id.listviewwishlist);
		IhmObjets.listeArticles.setContext(this);
		IhmObjets.listeArticles.refreshAdaptater(service.getListArticles());

		IhmObjets.listeArticles
				.setOnItemClickListener(new OnItemClickListener() {
					@SuppressWarnings("unchecked")
					public void onItemClick(AdapterView<?> a, View v,
							int position, long id) {
						// on récupère la HashMap contenant les infos de notre
						// item
						// (titre, description, img)
						HashMap<String, String> map = (HashMap<String, String>) IhmObjets.listeArticles
								.getItemAtPosition(position);
						showArticleAdder(new Integer(map.get(ID2)));
					}
				});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.whishlistmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.addlist:
			addArticleAdder();
			return true;
		case R.id.dellist:
			ajouterBoiteDeDialogue();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void ajouterBoiteDeDialogue() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(
				ETES_VOUS_SUR_DE_VOULOIR_SUPPRIMER_TOUTE_LA_LISTE_D_ARTICLE)
				.setCancelable(false)
				.setPositiveButton(OUI, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						service.delListArticles();
						IhmObjets.listeArticles.refreshAdaptater(service
								.getListArticles());
					}
				})
				.setNegativeButton(NON, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}

	@Override
	public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {

		switch (item.getItemId()) {
		case R.id.action_bar_add_article:
			addArticleAdder();
			break;
		default:
			return super.onHandleActionBarItemClick(item, position);
		}

		return true;
	}

	/**
	 * Launches the wishlist activity to add a new contact to the selected
	 * accont.
	 */
	protected void showArticleAdder(Integer id) {
		Intent i = new Intent(this, ShowArticle.class);
		i.putExtra(ID2, id);
		startActivity(i);
	}

	/**
	 * Launches the wishlist activity to add a new contact to the selected
	 * accont.
	 */
	protected void addArticleAdder() {
		startActivity(new Intent(this, AddArticle.class));
	}

}
